#ifndef SHENLAN_SIMPLE_LIGHTING
#define SHENLAN_SIMPLE_LIGHTING
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

#define shadow_offset      1  //用来调整投影的ST，0时投影在RT的占比较小，但可扩大的空间大，1时投影在RT的占比较大，但可扩大的空间小，
#define st_roleShadowTex   float4(0.0243, 0.06, 0.5, 0.615)


//拷贝自 Spine-SkeletonLit-ForwardPass-URP.hlsl LightweightLightVertexSimplified
float3 SimplifiedVertexLight(float3 positionWS)
{
    float3 normalWS_temp = float3(0.01, 1, 0.01);
    Light mainLight = GetMainLight();
    float3 attenuatedLightColor = mainLight.color * mainLight.distanceAttenuation;
    float3 diffuseLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS_temp);

    for (int i = 0; i < GetAdditionalLightsCount(); ++i)
    {
        Light light = GetAdditionalLight(i, positionWS);
        float3 attenuatedLightColor = light.color * light.distanceAttenuation ;
        diffuseLightColor += LightingLambert(attenuatedLightColor, abs(light.direction), normalWS_temp);
    }
    diffuseLightColor = clamp(diffuseLightColor,0.0,30.0);
    return diffuseLightColor;
}


//上面的方法去掉平行光和阴影，法线值调大
half3 SimplifiedVertexLight_Custom(float3 positionWS)
{
    half3 normalWS_temp = half3(0.01, 1, 0.01);
    Light mainLight = GetMainLight();
    half3 attenuatedLightColor = mainLight.color * mainLight.distanceAttenuation;
    half3 diffuseLightColor = LightingLambert(attenuatedLightColor, abs(mainLight.direction), normalWS_temp);
    for (int i = 0; i < GetAdditionalLightsCount(); ++i)
    {
        Light light = GetAdditionalLight(i, positionWS);
        half3 attenuatedLightColor = light.color * light.distanceAttenuation * 2;
        diffuseLightColor += LightingLambert(attenuatedLightColor, abs(light.direction), normalWS_temp*100);
    }
    diffuseLightColor = clamp(diffuseLightColor,0.0,30.0);
    return diffuseLightColor;
}

float3 SimplifiedVertexLight_Character(float3 positionWS)
{
    Light mainLight = GetMainLight();
    float3 attenuatedLightColor = mainLight.color * saturate(mainLight.distanceAttenuation);
    float3 diffuseLightColor = attenuatedLightColor;

    for (int i = 0; i < GetAdditionalLightsCount(); ++i)
    {
        Light light = GetAdditionalLight(i, positionWS);
        float3 attenuatedLightColor = light.color * saturate(light.distanceAttenuation);
        diffuseLightColor += attenuatedLightColor;
    }
    return diffuseLightColor;
}


half DifferenceLight_UI_Mul(float3 positionWSAndPercent, float smoothFator)//多光源差值效果

{
    float differenceRange;
    for (int i = 0; i < GetAdditionalLightsCount(); ++i)
    {
        int perObjectLightIndex = GetPerObjectLightIndex(i);
        #if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
            float4 lightPositionWS = _AdditionalLightsBuffer[perObjectLightIndex].position;
            float4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation;
        #else
            float4 lightPositionWS = _AdditionalLightsPosition[perObjectLightIndex];
            float4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[perObjectLightIndex];
        #endif
        
        //positionWSAndPercent.z ==>min((_ScreenParams.x * 9.0) / (float)(16.0 * _ScreenParams.y),1.0)
        //比例如果大于16/9时,则UIroot的缩放值仍然是按16/9的缩放值;小于16/9时,则UIroot的缩放值按W / H
        //例子：2400:1000 ==> 1/108 ==> 0.0092
        //例子：1920:1080 ==> 1/108 ==> 0.0092
        //例子：3000:3000 ==> 1/192 ==> 0.0052
        float lightRange = positionWSAndPercent.z / sqrt(abs(distanceAndSpotAttenuation.x));
        float differenceRange_temp = smoothstep(lightRange + smoothFator, lightRange - smoothFator, length(lightPositionWS.xy - positionWSAndPercent.xy));//10000为柔和度
        differenceRange += differenceRange_temp.r;
    }
    return differenceRange;
}

half2 DifferenceLight_UI_Simple(float3 positionWS)//单一光源差值效果

{
    #if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
        float4 lightPositionWS = _AdditionalLightsBuffer[0].position;
        half4 color = _AdditionalLightsBuffer[0].color;
        half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[0].attenuation;
        half4 spotDirection = _AdditionalLightsBuffer[0].spotDirection;
    #else
        float4 lightPositionWS = _AdditionalLightsPosition[0];
        half4 color = _AdditionalLightsColor[0];
        half4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[0];
        half4 spotDirection = _AdditionalLightsSpotDir[0];
    #endif

    float3 lightVector = lightPositionWS.xyz - positionWS * lightPositionWS.w;
    float distanceSqr = max(dot(lightVector, lightVector), HALF_MIN);
    half3 lightDirection = half3(lightVector * rsqrt(distanceSqr));
    half attenuation = DistanceAttenuation(distanceSqr, distanceAndSpotAttenuation.xy) * AngleAttenuation(spotDirection.xyz, lightDirection, distanceAndSpotAttenuation.zw);


    half2 differenceRange = 0;
    differenceRange.x = saturate(attenuation) * 100000;
    differenceRange.y = color.a;
    return differenceRange;
}

half3 SceneShadow(half2 uv, half factor, Texture2D tex, SamplerState tex_sm)
{
    half3 col = 1;
    #ifdef USE_ROLESHADOW_HIGHT
        half size = 0.0005;
        half4 shadow_0 = SAMPLE_TEXTURE2D(tex, tex_sm, uv + half2(1, 1) * size);
        half4 shadow_1 = SAMPLE_TEXTURE2D(tex, tex_sm, uv + half2(-1, 1) * size);
        half4 shadow_2 = SAMPLE_TEXTURE2D(tex, tex_sm, uv + half2(1, -1) * size);
        half4 shadow_3 = SAMPLE_TEXTURE2D(tex, tex_sm, uv + half2(-1, -1) * size);
        half4 shadow_4 = SAMPLE_TEXTURE2D(tex, tex_sm, uv);
        half4 shadow = (shadow_0 + shadow_1 + shadow_2 + shadow_3) * 0.17 + shadow_4 * 0.32;
        col.rgb = 1 - factor * lerp(shadow.r, shadow.g, _MainLightColor.a);
    #endif
    #ifdef USE_ROLESHADOW_LOW
        half4 shadow = SAMPLE_TEXTURE2D(tex, tex_sm, uv);
        col.rgb = 1 - factor * lerp(shadow.r, shadow.g, _MainLightColor.a);
    #endif
    return col;
}

// half4 FrontSceneAlpha(half4 c, float param)
// {
    //     c.a *= (1-param);
    //     return c;
    // }

    #if defined FRONT_SCENE
        #define SET_FS_PARAM         half _FrontSceneAlpha;
        #define SET_FS_FRAG(c)    c.a *= (1 - _FrontSceneAlpha);
    #else
        #define SET_FS_PARAM
        #define SET_FS_FRAG(c)
    #endif

#endif